﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Renamer.Properties;
using System.Text.RegularExpressions;

namespace Renamer.Forms
{
    public partial class FormRegexTest : Form
    {
        public string ResultRegularExpression { get { return txtRegExExpression.Text; } }

        public FormRegexTest(string regexPattern)
        {
            InitializeComponent();
            txtRegExExpression.Text = regexPattern;
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtRegExExpression.Text))
            {
                if (!String.IsNullOrEmpty(txtSampleString.Text))
                {
                    try
                    {
                        Regex r = new Regex(txtRegExExpression.Text);

                        if (r.IsMatch(txtSampleString.Text))
                        {
                            txtFoundMatch.ForeColor = Settings.Default.Visual_RegExTest_MatchFoundColor;
                            MatchCollection matches = r.Matches(txtSampleString.Text);
                            txtFoundMatch.Text = "";
                            foreach (Match match in matches)
                            { txtFoundMatch.Text += match.Value + "\n\r"; }

                        }
                        else
                        {
                            txtFoundMatch.ForeColor = Settings.Default.Visual_RegExTest_MatchNotFoundColor;
                            txtFoundMatch.Text = Settings.Default.Text_NoMatchesFound;
                        }
                        label3.Visible = true;
                        txtFoundMatch.Visible = true;
                    }
                    catch (ArgumentException ex) { MessageBox.Show(this, ex.Message, "Invalid Expression", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); }
                }
                else
                {
                    txtSampleString.BackColor = Settings.Default.Setting_VisualErrorTextColor;
                    MessageBox.Show(this, Settings.Default.Text_EnterSampleStringMessage, Settings.Default.Text_RegExTest, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                }
            }
            else
            {
                txtRegExExpression.BackColor = Settings.Default.Setting_VisualErrorTextColor;
                MessageBox.Show(this, Settings.Default.Text_EnterRegularExpression, Settings.Default.Text_RegExTest, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            }
        }

        private void txtRegExExpression_Click(object sender, EventArgs e)
        {
            txtRegExExpression.BackColor = System.Drawing.SystemColors.Window;
        }

        private void txtRegExExpression_KeyPress(object sender, KeyPressEventArgs e)
        {
            txtRegExExpression_Click(null, null);
        }

        private void txtSampleString_Click(object sender, EventArgs e)
        {
            txtSampleString.BackColor = System.Drawing.SystemColors.Window;
        }

        private void txtSampleString_KeyPress(object sender, KeyPressEventArgs e)
        {
            txtSampleString_Click(null, null);
        }
    }
}
